/**
 * @Description: http user-agent 解析
 * @author 若琳
 * @date 2021/8/13
*/


const UAParser = require("ua-parser-js");

const GetUserAgent = (req)=>{

    const uaParser = new UAParser(req.headers[`user-agent`]);
    //浏览器版本号
    let BrowserVersion =  uaParser.getResult().browser.version.substring(0,uaParser.getResult().browser.version.indexOf("."));
    //浏览器
    let browser = uaParser.getResult().browser.name + " "+BrowserVersion;
    //操作系统
    let OS = uaParser.getResult().os.name +" "+uaParser.getResult().os.version;

    return {browser:browser,OS:OS};
};

const GetClientIp = (req)=>{
    let ip =  req.headers['x-forwarded-for'] ||
        req.connection.remoteAddress ||
        req.socket.remoteAddress ||
        req.connection.socket.remoteAddress;
    return ip.match(/\d+\.\d+\.\d+\.\d+/)[0]
};



module.exports = {
    GetUserAgent:GetUserAgent,
    GetClientIp:GetClientIp
};